var ArticleReadWindow = {
	articleId:0
};
ArticleReadWindow.init = function(){
	windowManager.newWindow('article-read-container', 'articleRead');
}
ArticleReadWindow.open = function(){
	windowManager.windowShow('articleRead', ArticleReadWindow);
}
ArticleReadWindow.close = function(){
	windowManager.windowHide('articleRead');
	ArticleReadWindow._unbindEvents();
}

ArticleReadWindow._onOpened = function(){
	if ( 0 == ArticleReadWindow.articleId )
		throw ( 'ArticleReadWindow.read is not right.' );
	
	var art = Article.getInfo(ArticleReadWindow.articleId);
	
	$('#article-read-title').html(art.title);
	
	if ( null != art.content )
		$('#article-read-content').html(art.content);
	else
		Article.getContent(ArticleReadWindow.articleId, function( content ){
			$('#article-read-content').html(content);
		});
		
	if ( art.state == Article.STATE.LOCKED ){
		$('#article-read-action-lock').hide();
		$('#article-read-action-edit').hide();
	}
	else{
		$('#article-read-action-lock').show();
		$('#article-read-action-edit').show();
	}
		
	ArticleReadWindow._bindEvents();
	
	$('#article-read-information-return-back').button();
	$('#article-read-action-lock').button();
	$('#article-read-action-edit').button();
	$('#article-read-action-download').button();
	$('#article-read-action-move').button();
}
ArticleReadWindow._onLockClick = function(){
	Article.lock(ArticleReadWindow.articleId, function(){
		$('#article-read-action-lock').hide();
		$('#article-read-action-edit').hide();
	});
}
ArticleReadWindow._onEditClick = function(){
	ArticleReadWindow.close();

	ArticleEditWindow.articleId = ArticleReadWindow.articleId;
	ArticleEditWindow.returnBackTo = 'articleRead';
	ArticleEditWindow.open();
}
ArticleReadWindow._onDownloadClick = function(){
	Article.downloadById(ArticleReadWindow.articleId, function(url){
		window.open(url);
	});
}
ArticleReadWindow._onMoveClick = function(){
	$('#article-read-action-move').hide();
	
	var selector = $('<select>', {class:'ui-state-default ui-corner-all'});
	
	$('#article-read-action').append(selector);
	
	for ( var i=0; i<Article.category.categories.length; i++ )
		$('<option />', {value:Article.category.categories[i].id, html:Article.category.categories[i].name})
			.appendTo(selector);
	
	var art = Article.getInfo(ArticleReadWindow.articleId);
	var oldCategory = art.category;
	
	selector
		.val(oldCategory)
		.click(function(){return false;})
		.blur(function(){selector.remove(); $('#article-read-action-move').show();})
		.change(function(){
			selector.remove();
			$('#article-read-action-move').show();
			
			Article.move( ArticleReadWindow.articleId, selector.val(), function(){});
		});
}
ArticleReadWindow._onReturnBackClick = function(){
	ArticleReadWindow.close();

	ArticleMainWindow.open();
}

ArticleReadWindow._bindEvents = function(){
	$('#article-read-action-lock').click(function(){
		ArticleReadWindow._onLockClick();
	});
	
	$('#article-read-action-edit').click(function(){
		ArticleReadWindow._onEditClick();
	});
	
	$('#article-read-action-download').click(function(){
		ArticleReadWindow._onDownloadClick();
	});
	
	$('#article-read-action-move').click(function(){
		ArticleReadWindow._onMoveClick();
	});
	
	$('#article-read-information-return-back').click(function(){
		ArticleReadWindow._onReturnBackClick();
	});	
}
ArticleReadWindow._unbindEvents = function(){
	$('#article-read-action-lock').unbind('click');
	
	$('#article-read-action-edit').unbind('click');
	
	$('#article-read-action-download').unbind('click');
	
	$('#article-read-action-move').unbind('click');
	
	$('#article-read-information-return-back').unbind('click');
}
